<?php
namespace frontend\controllers;

use Yii;
use common\models\GameVersion;
use yii\helpers\BaseJson;
use common\models\Game;
use yii\filters\VerbFilter;

/**
 * 搜索控制器
 *
 * @author jois
 */
class SearchController extends BaseController
{
    // 定义每一页的数量
    public $limit = 10;

    /**
     * 请求方法过滤
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'game' => [
                        'post'
                    ],
                    'ajax-game-list' => [
                        'post'
                    ]
                ]
            ]
        ];
    }

    /**
     * 搜索游戏，回车之后加载
     */
    public function actionGame()
    {
        $key = Yii::$app->request->post('search_keyword', '');
        $games = GameVersion::find()->where("is_first=1");
        if (! $key) {
            $games = $games->joinWith([
                'game.types'
            ])->orderBy("game_id desc");
            $count = count($games->all());
            $games = $games->limit($this->limit)->all();
        } else {
            // 根据key找到game_id
            $game_ids = Game::find()->select("game_id")
                ->where("game_name like '%{$key}%'")
                ->column();
            $games = $games->where([
                'in',
                'game.game_id',
                $game_ids
            ])
                ->joinWith([
                'game.types'
            ])
                ->orderBy("game_id desc");
            $count = count($games->all());
            $games = $games->limit($this->limit)->all();
        }
        return $this->render('game', compact('games', 'key', 'count'));
    }

    /**
     * 异步获取游戏列表
     */
    public function actionAjaxGameList()
    {
        // 验证page参数
        $page = (int) Yii::$app->request->post('page', 0);
        $key = Yii::$app->request->post('key', "");
        if ($page > 1 && Yii::$app->request->isAjax) {
            // 开始获取数据
            if (!$key) {
                $games = GameVersion::find()->where("is_first=1")
                    ->joinWith([
                    'game.types'
                ])
                    ->orderBy("game.game_id desc")
                    ->offset(($page - 1) * $this->limit + 1)
                    ->limit($this->limit)
                    ->all();
            } else {
                // 根据key找到game_id
                $game_ids = Game::find()->select("game_id")
                    ->where("game_name like '%{$key}%'")
                    ->column();

                $games = GameVersion::find()->where("is_first=1")
                    ->where([
                    'in',
                    'game.game_id',
                    $game_ids
                ])
                    ->joinWith([
                    'game.types'
                ])
                    ->orderBy("game.game_id desc")
                    ->offset(($page - 1) * $this->limit + 1)
                    ->limit($this->limit)
                    ->asArray()
                    ->all();
            }
            return BaseJson::encode($games, true);
        }
        return BaseJson::encode('获取失败，参数或请求方法异常');
    }
}
